﻿using Abp.Application.Services.Dto;
using Abp.Domain.Repositories;
using Abp.Linq.Extensions;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Camc.Quality.RocketModel
{
    public class RocketModelAppService : QualityAppServiceBase
    {
        private readonly IRepository<RocketModelDesc, Guid> _rocketModelRepository;
        public RocketModelAppService(
            IRepository<RocketModelDesc, Guid> rocketModelRepository
            )
        {
            _rocketModelRepository = rocketModelRepository;
        }
        /// <summary>
        /// 火箭建模查询
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public async Task<PagedResultDto<RocketModelDto>> GetRocketModelList(GetRocketModelInput input)
        {
            var rocketModel = _rocketModelRepository.GetAll().Where(s=>s.IsUsed != RocketModelDesc.IsUseds.已删除);
            if (!string.IsNullOrEmpty(input.Model))
            {
                rocketModel = rocketModel.Where(d => d.Model.Contains(input.Model));
            }
            if (!string.IsNullOrEmpty(input.LotNo))
            {
                rocketModel = rocketModel.Where(d => d.LotNo.Contains(input.LotNo));
            }
            if (!string.IsNullOrEmpty(input.DrawingNo))
            {
                rocketModel = rocketModel.Where(d => d.DrawingNo.Contains(input.DrawingNo));
            }
            var count = rocketModel.Count();
            rocketModel = rocketModel.OrderByDescending(e => e.CreationTime).PageBy(input);
            var result = ObjectMapper.Map<List<RocketModelDto>>(rocketModel);
            return new PagedResultDto<RocketModelDto>(count, result);
        }
        /// <summary>
        /// 火箭建模新增和修改接口，根据是否传输id判断
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public async Task CreateOrUpdateRocketMode(RocketModelDto input)
        {
            if (input.Id == Guid.Empty)
            {
                await CreateRocketMode(input);
            }
            else
            {
                await UpdateRocketMode(input);
            }
        }
        /// <summary>
        /// 火箭建模新增
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        private async Task CreateRocketMode(RocketModelDto input)
        {
            var rocketModel = new RocketModelDesc()
            {
                Id = Guid.NewGuid(),
                Model = input.Model,
                LotNo = input.LotNo,
                DrawingNo = input.DrawingNo,
                IsUsed = (RocketModelDesc.IsUseds)input.IsUsed
            };
            await _rocketModelRepository.InsertAsync(rocketModel);
        }
        /// <summary>
        /// 火箭建模修改
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        private async Task UpdateRocketMode(RocketModelDto input)
        {
            var rocketModel = _rocketModelRepository.FirstOrDefault(s=>s.Id == input.Id);

            rocketModel.Model = input.Model;
            rocketModel.LotNo = input.LotNo;
            rocketModel.DrawingNo = input.DrawingNo;
            await _rocketModelRepository.UpdateAsync(rocketModel);
        }
        /// <summary>
        /// 火箭建模删除
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task DeleteRocketMode(Guid id)
        {
            var rocketModel = await _rocketModelRepository.FirstOrDefaultAsync(c => c.Id == id);
            if (rocketModel != null)
            {
                rocketModel.IsUsed = RocketModelDesc.IsUseds.已删除;
                await _rocketModelRepository.UpdateAsync(rocketModel);
            }
        }
    }
}
